package com.hello.system.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.hello.common.log.annotation.Log;
import com.hello.common.log.enums.BusinessType;
import com.hello.common.security.annotation.RequiresPermissions;
import com.hello.system.domain.RentalDeviceInventory;
import com.hello.system.service.IRentalDeviceInventoryService;
import com.hello.common.core.web.controller.BaseController;
import com.hello.common.core.web.domain.AjaxResult;
import com.hello.common.core.utils.poi.ExcelUtil;
import com.hello.common.core.web.page.TableDataInfo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * 设备库存Controller
 * 
 * @author hello
 * @date 2025-07-20
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/inventory")
public class RentalDeviceInventoryController extends BaseController
{
    private final IRentalDeviceInventoryService rentalDeviceInventoryService;

    /**
     * 查询设备库存列表
     */
    // @RequiresPermissions("system:inventory:list")
    @GetMapping("/list")
    public TableDataInfo list(RentalDeviceInventory rentalDeviceInventory)
    {
        log.info("======== 查询设备库存列表请求 ========");
        log.info("请求参数: {}", rentalDeviceInventory);
        
        startPage();
        List<RentalDeviceInventory> list = rentalDeviceInventoryService.selectRentalDeviceInventoryList(rentalDeviceInventory);
        TableDataInfo result = getDataTable(list);
        
        log.info("查询结果: 总记录数={}, 当前页数据量={}", result.getTotal(), list.size());
        log.info("======== 查询设备库存列表完成 ========");
        
        return result;
    }

    /**
     * 导出设备库存列表
     */
    // @RequiresPermissions("system:inventory:export")
    @Log(title = "设备库存", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, RentalDeviceInventory rentalDeviceInventory)
    {
        log.info("======== 导出设备库存数据请求 ========");
        log.info("导出条件: {}", rentalDeviceInventory);
        
        List<RentalDeviceInventory> list = rentalDeviceInventoryService.selectRentalDeviceInventoryList(rentalDeviceInventory);
        log.info("导出数据量: {} 条记录", list.size());
        
        ExcelUtil<RentalDeviceInventory> util = new ExcelUtil<RentalDeviceInventory>(RentalDeviceInventory.class);
        util.exportExcel(response, list, "设备库存数据");
        
        log.info("======== 设备库存数据导出完成 ========");
    }

    /**
     * 获取设备库存详细信息
     */
    // @RequiresPermissions("system:inventory:query")
    @GetMapping(value = "/{inventoryId}")
    public AjaxResult getInfo(@PathVariable("inventoryId") Long inventoryId)
    {
        log.info("======== 获取设备库存详情请求 ========");
        log.info("库存ID: {}", inventoryId);
        
        RentalDeviceInventory inventory = rentalDeviceInventoryService.selectRentalDeviceInventoryByInventoryId(inventoryId);
        log.info("查询结果: {}", inventory != null ? "找到库存信息" : "未找到对应库存");
        
        log.info("======== 获取设备库存详情完成 ========");
        return success(inventory);
    }

    /**
     * 新增设备库存
     */
    // @RequiresPermissions("system:inventory:add")
    @Log(title = "设备库存", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody RentalDeviceInventory rentalDeviceInventory)
    {
        log.info("======== 新增设备库存请求 ========");
        log.info("新增数据: {}", rentalDeviceInventory);
        
        int result = rentalDeviceInventoryService.insertRentalDeviceInventory(rentalDeviceInventory);
        log.info("新增结果: {}", result > 0 ? "成功" : "失败");
        
        log.info("======== 新增设备库存完成 ========");
        return toAjax(result);
    }

    /**
     * 修改设备库存
     */
    // @RequiresPermissions("system:inventory:edit")
    @Log(title = "设备库存", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody RentalDeviceInventory rentalDeviceInventory)
    {
        log.info("======== 修改设备库存请求 ========");
        log.info("修改数据: {}", rentalDeviceInventory);
        
        int result = rentalDeviceInventoryService.updateRentalDeviceInventory(rentalDeviceInventory);
        log.info("修改结果: {}", result > 0 ? "成功" : "失败");
        
        log.info("======== 修改设备库存完成 ========");
        return toAjax(result);
    }

    /**
     * 删除设备库存
     */
    // @RequiresPermissions("system:inventory:remove")
    @Log(title = "设备库存", businessType = BusinessType.DELETE)
	@DeleteMapping("/{inventoryIds}")
    public AjaxResult remove(@PathVariable Long[] inventoryIds)
    {
        log.info("======== 删除设备库存请求 ========");
        log.info("删除库存ID列表: {}", java.util.Arrays.toString(inventoryIds));
        log.info("删除数量: {} 个库存记录", inventoryIds.length);
        
        int result = rentalDeviceInventoryService.deleteRentalDeviceInventoryByInventoryIds(inventoryIds);
        log.info("删除结果: {}", result > 0 ? "成功" : "失败");
        
        log.info("======== 删除设备库存完成 ========");
        return toAjax(result);
    }
}
